﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OEMS
{
    /// <summary>
    /// User management form
    /// </summary>
    /// <author>Nick Lauryssen</author>
    public partial class GebruikersEditForm : Form
    {
        private String[] users;
        private String curUser;
        private String selectedUser;

        public GebruikersEditForm(String currentUser)
        {
            InitializeComponent();
            users = new String[Properties.Settings.Default.Users.Count];
            Properties.Settings.Default.Users.CopyTo( users, 0 );
            lbUsers.DataSource = users;
            curUser = currentUser;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            GebruikersForm form = new GebruikersForm(this);
            form.Show();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            selectedUser = Convert.ToString(lbUsers.SelectedValue);
            int i = 0;
            foreach (String user in users)
            {
                if (!selectedUser.Equals(curUser))
                {
                    if (selectedUser.Equals(user))
                    {
                        Properties.Settings.Default.Users.RemoveAt(i);
                        Properties.Settings.Default.Passwords.RemoveAt(i);
                        Properties.Settings.Default.Rechten.RemoveAt(i);
                        updateList();
                        break;
                    }
                    i++;
                }
                else
                {
                    MessageBox.Show("Deze gebruiker kan niet worden verwijderd omdat hij ingelogd is.", "Gebruiker verwijderen", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Properties.Settings.Default.Reload();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            selectedUser = Convert.ToString(lbUsers.SelectedItem);
            GebruikersForm form = new GebruikersForm(this, selectedUser);
            form.Show();
        }

        /// <summary>
        /// Zorgt dat de lijst van users altijd up to date is
        /// </summary>
        /// <author>Nick Lauryssen</author>
        public void updateList()
        {
            users = new String[Properties.Settings.Default.Users.Count];
            Properties.Settings.Default.Users.CopyTo(users, 0);
            lbUsers.DataSource = users;
        }
    }
}
